package com.example.myapplication01.jetpack.basketball;

import static com.example.myapplication01.jetpack.basketball.Constant.DATA_KEY_A;
import static com.example.myapplication01.jetpack.basketball.Constant.DATA_KEY_B;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.SavedStateHandle;
import androidx.lifecycle.ViewModel;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class ScoreViewModel extends ViewModel {
    /*private MutableLiveData<Integer> scoreA;
    private MutableLiveData<Integer> scoreB;*/
    private final Deque<Map<Integer, Integer>> scoreStack = new ArrayDeque<>();
    private final Integer TEAM_A = 0;
    private final Integer TEAM_B = 1;
    private final SavedStateHandle savedStateHandle;

    public ScoreViewModel(SavedStateHandle savedStateHandle) {
        this.savedStateHandle = savedStateHandle;
    }


    public MutableLiveData<Integer> getScoreA() {
        if (!savedStateHandle.contains(DATA_KEY_A)) {
            savedStateHandle.set(DATA_KEY_A, 0);
        }
        return savedStateHandle.getLiveData(DATA_KEY_A, 0);
    }
    public MutableLiveData<Integer> getScoreB() {
        if (!savedStateHandle.contains(DATA_KEY_B)) {
            savedStateHandle.set(DATA_KEY_B, 0);
        }
        return savedStateHandle.getLiveData(DATA_KEY_B, 0);
    }

    // 累加分数
    public void addA(int score) {
        savedStateHandle.set(DATA_KEY_A, (Integer) savedStateHandle.getLiveData(DATA_KEY_A).getValue() + score);
        Map<Integer, Integer> map = new HashMap<>(1);
        map.put(TEAM_A, score);
        scoreStack.push(map);
    }
    public void addB(int score) {
        savedStateHandle.set(DATA_KEY_B, (Integer) savedStateHandle.getLiveData(DATA_KEY_B).getValue() + score);
        Map<Integer, Integer> map = new HashMap<>(1);
        map.put(TEAM_B, score);
        scoreStack.push(map);
    }

    // 撤销分数
    public void undo() {
        if (Objects.equals(savedStateHandle.getLiveData(DATA_KEY_A).getValue(), 0) &&
                Objects.equals(savedStateHandle.getLiveData(DATA_KEY_B).getValue(), 0)) return;
        Map<Integer, Integer> map = scoreStack.pollFirst();

        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        for (Map.Entry<Integer, Integer> entry : entries) {
            Integer key = entry.getKey();
            if (Objects.equals(TEAM_A, key)) {
                savedStateHandle.set(DATA_KEY_A, (Integer) savedStateHandle.getLiveData(DATA_KEY_A).getValue() - entry.getValue());
            } else {
                savedStateHandle.set(DATA_KEY_B, (Integer) savedStateHandle.getLiveData(DATA_KEY_B).getValue() - entry.getValue());
            }
        }
    }

    // 刷新分数
    public void refresh() {
        while (!scoreStack.isEmpty()) {
            scoreStack.pollFirst();
        }
        savedStateHandle.set(DATA_KEY_A, 0);
        savedStateHandle.set(DATA_KEY_B, 0);
    }

}
